package java_thinking.unit_14._14_3;

import java_thinking.unit_14.pets.*;

import java.util.HashMap;
import java.util.Map;

import static java_thinking.net.mindview.util.Print.printnb;

/**
 * @Description : 对pet进行计数
 * @author: cww
 * @DateTime: 2019/3/28 19:49
 */

public class PetCount {
    /**
     * 对比两种计数方法
     * 内部类方式:继承了HashMap，PetCounter新建时也会默认执行父类的构造器，
     * 也就有了一个HashMap的对象的引用，并且子类可以引用父类的所有方法
     * 就不用再传入map了，妙！
     * 灵魂：需要使用到一个类方法，但是又要根据入参执行原本没有的功能，继承他，扩展他
     */
    static class PetCounter extends HashMap<String,Integer>{
        public void count(String type){
            Integer quantity = get(type);
            if (quantity == null){
                this.put(type,1);
            } else {
                put(type,quantity+1);
            }
        }
    }
    /**
     * 对比两种计数方法
     * @param map
     * @param type
     */
    static void saveCount (Map<String,Integer> map,String type){
        Integer integer = map.get(type);
        if (integer==null){
            map.put(type,1);
        }else {
            map.put(type,integer+1);
        }
    }

    public static void countPets(PetCreator creator) {
        PerCounter1 counter= new PerCounter1();
        Map<String,Integer> map = new HashMap<>();

        for(Pet pet : creator.createArray(20)) {
            // List each individual pet:
            printnb(pet.getClass().getSimpleName() + " ");
            if(pet instanceof Pet) {
                counter.count("Pet");
                saveCount(map, "Pet");
            }
            if(pet instanceof Dog) {
                counter.count("Dog");
                saveCount(map, "Dog");
            }
            if(pet instanceof Mutt) {
                counter.count("Mutt");
                saveCount(map, "Mutt");
            }
            if(pet instanceof Pug) {
                counter.count("Pug");
                saveCount(map, "Pug");
            }
            if(pet instanceof Cat) {
                counter.count("Cat");
                saveCount(map, "Cat");
            }
            if(pet instanceof EgyptianMau) {
                counter.count("EgyptianMau");
                saveCount(map, "EgyptianMau");
            }
            if(pet instanceof Manx) {
                counter.count("Manx");
                saveCount(map, "Manx");
            }
            if(pet instanceof Cymric) {
                counter.count("Cymric");
                saveCount(map, "Cymric");
            }
            if(pet instanceof Rodent) {
                counter.count("Rodent");
                saveCount(map, "Rodent");
            }
            if(pet instanceof Rat) {
                counter.count("Rat");
                saveCount(map, "Rat");
            }
            if(pet instanceof Mouse) {
                counter.count("Mouse");
                saveCount(map, "Mouse");
            }
            if(pet instanceof Hamster) {
                counter.count("Hamster");
                saveCount(map, "Hamster");
            }
        }
        // Show the counts:
        System.out.println();
        System.out.println(counter);
        System.out.println("******************");
        System.out.println(map);
    }


    public static void main(String[] args) {
        countPets(new ForNameCreator());
    }

    static class PerCounter1 extends HashMap<String,Integer> {
        public void count(String type){
            Integer integer = get(type);
            if (integer==null){
                put(type,1);
            }else {
                put(type,integer+1);
            }
        }
    }
}
